|
|
|
|
|
|
|
Listing 20.10 SUBCLASS.ZIPForm-Level Code for the Subclass Example |
|
|
|
|
|
|
|
|
Private Sub cmdHook_Click()
Hook
End Sub
Private Sub cmdUnhook_Click()
Unhook
End Sub
Private Sub Form_Load()
gHW = Me.hwnd
End Sub |
|
|
|
|
|
|
|
|
To look at these listings step by step, start with the Hook subroutine in Listing 20.9. Hook passes the address of the callback function, WindowProc, to the SetWindowLong API. SetWindowLong makes WindowProc the message handler for the form, replacing whatever VB message handler was already in place. The address of the VB message handler being replaced is returned and stored in lpPrevWndProc. UnHook calls SetWindowLong again with this variable to return control to the standard message handler. The Hook and Unhook subroutines act as a kind of switch that controls where Windows messages are sent. When Hook is called, all Windows messages are routed to WindowProc. |
|
|
|
|
|
|
|
|
WindowProc accepts four parameters: the window handle of the form and the three parameters associated with each message. The first line of code in WindowProc simply prints all this information in the Immediate window. The second line calls yet another API function, CallWindowProc. CallWindowProc passes the message information on to the standard Windows message handler, whose address is available in the lpPrevWndProc variable. |
|
|
|
|
|
|
|
|
So, in effect, the "custom" message handler WindowProc really doesn't do anything but print the message and call the message handler that would have been called anyway. A sample run is shown in Figure 20.5. |
|
|
|
|
|
|
|
|
Figure 20.5
A sample program uses subclassing techniques
to install its own Windows message handler. |
|
|
|
|
|
|
|
|
To really prove that you are actually intercepting messages, you need to make WindowProc do something special, like throw away a message. Go back to the MouseMove event example. Place the following line of code in the form's MouseMove event procedure: |
|
|
|
|
|